package com.jims.master.modules.csm.web; /**
 * jims
 */

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpClassDict;
import com.jims.master.modules.csm.service.ExpClassDictService;
import com.jims.master.modules.utils.ExpClassTreeTable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 消耗品类别字典Controller
 *
 * @author fengyg
 * @version 2017-05-04
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expClassDict")
public class ExpClassDictController extends BaseController {

    @Autowired
    private ExpClassDictService expClassDictService;

    @ModelAttribute
    public ExpClassDict get(@RequestParam(required = false) String id) {
        ExpClassDict entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = expClassDictService.get(id);
        }
        if (entity == null) {
            entity = new ExpClassDict();
        }
        return entity;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(ExpClassDict expClassDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<ExpClassDict> list = expClassDictService.findList(expClassDict);
        List<ExpClassDict> treeList = new ExpClassTreeTable(list).buildTree();
        if (treeList.size() == 0) {
            treeList = list;
        }
        model.addAttribute("list", treeList);
        return "modules/csm/expClassDictIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(ExpClassDict expClassDict, Model model) {
        if (null != expClassDict.getClassCode() && "".equals(expClassDict.getClassCode())) {
            expClassDict.setClassCode(expClassDict.getClassCode().substring(expClassDict.getClassCode().length() - 2, 2));
        }
        model.addAttribute("expClassDict", expClassDict);
        return "modules/csm/expClassDictForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(ExpClassDict expClassDict, Model model) {
        StringData data = new StringData();
        int i = expClassDictService.saveClassDict(expClassDict);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存消耗品类别字典成功");
        } else {
            data.setCode("error");
            data.setData("保存消耗品类别字典失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(ExpClassDict expClassDict) {
        StringData data = new StringData();
        int i = 0;
        if (StringUtils.isNotBlank(expClassDict.getId())) {
            int count = expClassDictService.findByParentId(expClassDict.getId());
            if (count > 0) {
                i = -1;
            } else {
                i = expClassDictService.delete(expClassDict);
            }
        }

        if (i > 0) {
            data.setCode("success");
            data.setData("删除消耗品类别字典表成功");
        } else if (i < 0) {
            data.setCode("info");
            data.setData("该类别下有子类别，不能删除");
        } else {
            data.setCode("error");
            data.setData("删除消耗品类别字典表失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        ExpClassDict expClassDict = new ExpClassDict();
        expClassDict.setDelFlag("0");
        List<ExpClassDict> list = expClassDictService.findList(expClassDict);
        if (list != null && list.size() > 0) {
            for (ExpClassDict dc : list) {
                if ((StringUtils.isBlank(extId) || (extId != null && !extId.equals(dc.getId()) && dc.getParentIds().indexOf("." + extId + ".") == -1))) {
                    Map<String, Object> map = Maps.newHashMap();
                    map.put("id", dc.getId());
                    map.put("pId", dc.getParent().getId());
                    map.put("pIds", dc.getParentIds());
                    map.put("name", dc.getClassName());
                    mapList.add(map);
                }
            }
        }
        return mapList;
    }

    /**
     * @param expClassDict 传递参数
     * @return StringData    返回类型
     * @throws
     * @Title: validUnique
     * @Desciption: (验证药品代码唯一性)
     * @author fengyg
     * @date 2017-05-04
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "validUnique")
    @ResponseBody
    public StringData validUnique(ExpClassDict expClassDict) {
        StringData data = new StringData();
        int i = expClassDictService.validUnique(expClassDict);
        if (i <= 0) {
            data.setCode("success");
//			data.setData("删除药品类别字典表成功");
        } else {
            data.setCode("error");
            data.setData("该类别代码已经存在，请重新输入");
        }
        return data;
    }

}